<?php

namespace Xx2025\Kingdee\Erp\Service;

use Xx2025\Kingdee\Erp\BaseResponse;
use Xx2025\Kingdee\Erp\Client;
use Xx2025\Kingdee\Erp\Response\GyErpSupplierGet;

class Supplier {


    /**新增供应商
     * @param $supplierCode
     * @param $supplierName
     * @return BaseResponse
     */
    public static function newSupplier($supplierCode, $supplierName) :BaseResponse
    {
        if(!$supplierCode) throw new \Exception("supplier Code is required");
        if(!$supplierName) throw new \Exception("supplier Name is required");

        $params = [
            'supplier_code'=>$supplierCode,
            'supplier_name'=>$supplierName,
        ];

        return Client::request('gy.erp.supplier.add', $params);

    }

    /**供应商是否存在
     * @param string $supplierCode
     * @return bool
     * @throws \Throwable
     */
    public static function existSupplier(string $supplierCode) :bool
    {
        if(!$supplierCode) throw new \Exception("Supplier code is required");
        $params = [
            'code'=>$supplierCode,
        ];
        $res = Client::request('gy.erp.supplier.get', $params);

        $resData = $res->getResponseContents();

        if(isset($resData['supplier_list'][0])){
            $info = $resData['supplier_list'][0];
            if($info['code'] == $supplierCode){
                return true;
            }
        }
        return false;
    }

    public static function getSupplierCodeList($code = '', $pageNo = 1, $pageSize = 10)
    {
        $params = [
            'page_no'=>$pageNo,
            'page_size'=>$pageSize,
        ];
        if(!$code) {
            $params['code'] = $code;
        }
        $response = GyErpSupplierGet::getInstance();
        Client::request('gy.erp.supplier.get', $params, $response);

        $resData = $response->getResponseContents();
        $list = [];
        foreach ($resData['supplier_list'] as $supplier) {
            $list[] = $supplier['code'];
        }
        $response->setSupplierCodeList($list);
        return $response;

    }



}